var UserName = document.getElementById("UserName");
var Password = document.getElementById("Password");
var LoginButton = document.getElementById("LoginButton");
var RegisterUserName = document.getElementById("RegisterUserName");
var RegisterPassword = document.getElementById("RegisterPassword");
var RegisterPhone = document.getElementById("RegisterPhone");
var UserNameText = ''
var CheckCode = ''

// isLoginFun();
//登录
function login() {
    if (!UserName.value) {
        layer.msg("请先输入用户名", {icon: 2});
        UserName.focus();
        return;
    }
    if (!Password.value) {
        layer.msg("请先输入密码", {icon: 2});
        Password.focus();
        return;
    }
    UserNameText = $("#UserName").val();
    var PasswordText = $("#Password").val();
    var da = {
        "username": UserNameText,
        "password": PasswordText
    };
    commonAjaxPost(true, "/admin/userLogins", da, loginSuccess)
}

function getMIBAOQuestion() {
    var user = $("#MIBAOUserName").val();
    if (!user) {
        layer.msg("请输入用户名", {icon: 2});
        return;
    }
    //显示as1
    $("#Question1").show("slow");
    $("#as1").show("slow");
    $("#Question2").show("slow");
    $("#as2").show("slow");
}

function MIBAOCheck() {
    var MIBAOQ1 = $("#Question1").val();
    var MIBAOQ2 = $("#Question2").val();
    var MIBAOA1 = $("#as1").val();
    var MIBAOA2 = $("#as2").val();
    var user = $("#MIBAOUserName").val();
    var da = {
        "username": user,
        "question1": MIBAOQ1,
        "question2": MIBAOQ2,
        "answer1": MIBAOA1,
        "answer2": MIBAOA2
    };
    commonAjaxPost(true, "/admin/userMIBAOCheck", da, function (result) {
        if (result.code === '666') {
            layer.msg(result.message, {icon: 1});
            $("#MIBAOUserName").hide("fast");
            $("#Question1").hide("fast");
            $("#Question2").hide("fast");
            $("#as1").hide("fast");
            $("#as2").hide("fast");
            var da = {
                "phoneNum": user,
                "password": "000000"
            };
            commonAjaxPost(true, "/admin/modifyUserpassword", da, function (result) {
                if (result.code === '666') {
                    alert("密码重置成功，新密码为000000");
                    window.location.href = "login.html";
                } else {
                    layer.msg(result.message, {icon: 2});
                }
            })

        } else {
            layer.msg(result.message, {icon: 2});
        }
    })
}

//手机号+密码登录，后期补充验证码
function phoneLogin() {
    var checkCodeText = $("#LoginCheckCode").val();
    if (checkCodeText != CheckCode) {
        layer.msg("验证码错误", {icon: 2});
        return;
    }
    var PhoneText = $("#phone").val();
    var da = {
        "phoneNum": PhoneText,
    }
    commonAjaxPost(true, "/admin/userLogins", da, loginSuccess)
}

function turnphone() {
    $("#UserName").hide("fast");
    $("#Password").hide("fast");
    $("#LoginButton").hide("fast");
    $("#forgettext").hide("fast");
    $("#phone").show("slow");
    $("#phoneLogindiv").show("slow");
    $("#LoginButton2").show("slow");

}

function turnpassword() {
    $("#phone").hide("fast");
    $("#phoneLogindiv").hide("fast");
    $("#LoginButton2").hide("fast");
    $("#forgettext").show("slow");
    $("#UserName").show("slow");
    $("#Password").show("slow");
    $("#LoginButton").show("slow");
}

//找回密码验证
function resetPassword() {
    //获取id为RegisterPhone标签的值
    var RegisterPhoneText = $("#RegisterPhone").val();
    if (RegisterPhoneText.length !== 11) {
        layer.msg("手机号格式不正确", {icon: 2});
        return;
    }
    var ps1Text = $("#ps1").val();
    var ps2Text = $("#ps2").val();
    var userCheckCode = $("#UserCkeckCode").val();
    if (userCheckCode !== CheckCode) {
        layer.msg("验证码错误", {icon: 2});
        return;
    }
    if (!ps1Text) {
        layer.msg("请设置新密码", {icon: 2});
        return;
    }
    if (ps1Text !== ps2Text) {
        layer.msg("两次密码不一致", {icon: 2});
        return;
    }
    //修改数据库密码
    var da = {
        "phoneNum": RegisterPhoneText,
        "password": ps1Text
    };
    commonAjaxPost(true, "/admin/modifyUserpassword", da, function (result) {
        if (result.code === '666') {
            layer.msg("密码重置成功", {icon: 1});
            window.location.href = "login.html";
        } else {
            layer.msg(result.message, {icon: 2});
        }
    })
}

//发送验证码并接受后台返回的验证码
function sendMsg(name) {
    var PhoneText = name;
    if (PhoneText.length !== 11) {
        layer.msg("手机号格式不正确", {icon: 2});
        return;
    }
    var da = {
        "phoneNum": PhoneText
    };
    //显示时间
    var time = 60;
    var timer = setInterval(function () {
        if (time > 0) {
            time--;
            $("#CheckmesButton").text(time + "s");
            $("#CheckmesButton").attr("disabled", true);
            $("#LogincheckmesButton").text(time + "s");
            $("#LogincheckmesButton").attr("disabled", true);
        } else {
            $("#CheckmesButton").text("重新发送");
            $("#CheckmesButton").attr("disabled", false);
            $("#LogincheckmesButton").text("重新发送");
            $("#LogincheckmesButton").attr("disabled", false);
            clearInterval(timer);
        }
    }, 1000)
    commonAjaxPost(true, "/admin/sendMsg", da, function (result) {
        if (result.code === '666') {
            layer.msg(result.message, {icon: 1});
            CheckCode = result.data;
            $("#ps1").show("slow");
            $("#ps2").show("slow");
        } else {
            layer.msg(result.message, {icon: 2});
            time = 0;
        }
    })
}


function register() {
    if (!RegisterUserName.value) {
        layer.msg("请先输入用户名", {icon: 2});
        RegisterPhone.focus();
        return;
    }
    //用户名长度限制
    if (RegisterUserName.value.length > 10) {
        layer.msg("用户名长度为1-10位", {icon: 2});
        return;
    }
    if (!RegisterPassword.value) {
        layer.msg("请先输入密码", {icon: 2});
        RegisterPhone.focus();
        return;
    }
    if (!RegisterPhone.value) {
        layer.msg("请先输入手机号", {icon: 2});
        RegisterPhone.focus();
        return;
    }
    var RegisterUserNameText = $("#RegisterUserName").val();
    var RegisterPassWordText = $("#RegisterPassword").val();
    var RegisterPhoneText = $("#RegisterPhone").val();
    //正则表达式验证手机号
    if (RegisterPhoneText.length !== 11) {
        layer.msg("手机号格式不正确", {icon: 2});
        return;
    }
  //获取select标签选择的值
  var selectValueText = $("#RegisterSelect").val();
  var da1 = {
    "username":RegisterUserNameText,
    "password":RegisterPassWordText,
    "phoneNum":RegisterPhoneText,
    "opusername":RegisterUserNameText,
    "roleId":selectValueText,
    "startTime": "1600000000000",
    "stopTime": "1600000000000"
  };
  commonAjaxPost(true, "/admin/addUserInfo", da1, function(result){
    if (result.code === '666'){
        layer.msg("注册成功", {icon: 1});
      if(selectValueText===0){
        //window.location.href = "TenantsManage.html"
      }else if(selectValueText===1){
       // window.location.href = "userManage.html"
      }else if(selectValueText===2){
      //  window.location.href = "myQuestionnaires.html"
      }else {
      //  window.location.href = "reviewAnswerQuestions.html"
      }
      deleteCookie('userName');
      setCookie('isLogin', '1');
      setCookie('userId', result.data.id);
      setCookie('userName', RegisterUserNameTest);
    }else{
      layer.msg(result.message, {icon: 2});
    }
  })
}

//登录成功回调
function loginSuccess(result) {
  if (result.code === '666') {
      console.log(UserNameText)
    result.data[0].roleId === "0" ?
        window.open("http://localhost:8080/#/data?id=" + UserNameText, '_top')
        // window.location.href = "TenantsManage.html"
       : result.data[0].roleId === "1" ?
            tenants()
                // +UserNameText
                : result.data[0].roleId === "2" ?
                    enduser()
                    : window.location.href = "reviewAnswerQuestions.html";
        layer.msg("登录成功", {icon: 1});
        //必须要放在前面才能跳转
        //layer.msg(result.message, {icon: 1});
        deleteCookie('userName');
        deleteCookie('power');
        deleteCookie('userId');
        setCookie('isLogin', '1');
        setCookie('userId', result.data.id);
        setCookie('userName', UserNameText);
        setCookie('power', result.data.role);
    } else {
        layer.msg(result.message, {icon: 2});
        // $('#errorMsgBox').show()
        // $('#errorMsgBox').text(result.message)
    }
}

function tenants() {

    window.open("http://localhost:8181/#/endUser?id=" + UserNameText, '_top');
    // window.open("http://localhost:8080/#/data?id=" + UserNameText, '_top');
    // window.localStorage.setItem("tenantsName",UserNameText)
    // open("http://localhost:8181/#/endUser?id=" + UserNameText, '_self').close();
}

function enduser() {
    window.open("http://localhost:8181/#/respond?id=" + UserNameText);
    // window.localStorage.setItem("tenantsName",UserNameText)
    // open("http://localhost:8181/#/endUser?id=" + UserNameText, '_self').close();
}

//回车事件
$(document).keydown(function (event) {
    if (event.keyCode == 13) {
        login();
    }
});
